<?php
namespace App\Repositories\Admin;

use App\Models\Admin\CategoryModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\StaffModel;
use App\Models\Admin\StaffOptionModel;
use App\Models\Admin\UserModel;
use App\Repositories\BaseRepository;

class StaffRepository extends BaseRepository
{
    protected $repository;
    protected $option;
    protected $user;

    public function __construct()
    {
        $this->repository = new StaffModel();
        $this->option = new StaffOptionModel();
        $this->user = new UserModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList()
    {
        $models = $this->repository
            ->orderBy('id','desc')
            ->paginate(10);
        $models->limit = 10;
        if (count($models)) {
            foreach ($models as $key => $model) {
                if ($skillStr=$model->skill) {
                    $skills = explode(',',$skillStr);
                    $models[$key]['skills'] = $skills;
                    $models[$key]['skills'] = $skills;
                } else {
                    $models[$key]['skills'] = array();
                }
                if ($hobbyStr=$model->hobby) {
                    $hobbys = explode(',',$hobbyStr);
                    $models[$key]['hobbys'] = $hobbys;
                    $models[$key]['hobbys'] = $hobbys;
                } else {
                    $models[$key]['hobbys'] = array();
                }
                $models[$key]['cateName'] = $this->getCateNameByCate($model->cate);
            }
        }
        return $models;
    }

    /**
     * Web端
     */
    public function getWebIndexList()
    {
        return $this->repository
            ->select('skill','realname','image')
            ->orderBy('id','asc')
            ->skip(0)
            ->take(3)
            ->get();
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function setModifyByUid($data,$uid)
    {
        $data['updated_at'] = time();
        return $this->repository->where('uid',$uid)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 通过UID获取
     */
    public function getShowOneByUid($uid)
    {
        return $this->repository->where('uid',$uid)->first();
    }

    /**
     * 后台获取详情
     */
    public function getAdminShow($id)
    {
        $model = $this->getShowOne($id);
        if ($skillStr=$model->skill) {
            $skills = explode(',',$skillStr);
            $model->skills = $skills;
            $skillNames = array();
            foreach ($skills as $key => $skill) {
                $skillNames[$key] = $this->getCateNameById($skill);
            }
            $model->skillNames = $skillNames;
        } else {
            $model->skills = array();
            $model->skillNames = array();
        }
        if ($hobbyStr=$model->hobby) {
            $hobbys = explode(',',$hobbyStr);
            $model->hobbys = $hobbys;
            $hobbyNames = array();
            foreach ($hobbys as $key => $hobby) {
                $hobbyNames[$key] = $this->getCateNameById($hobby);
            }
            $model->hobbyNames = $hobbyNames;
        } else {
            $model->hobbys = array();
            $model->hobbyNames = array();
        }
        $userModel = $this->user->where('id',$model->uid)->first();
        $model->mobile = $userModel->phone;
        $model->nickname = $userModel->name;
        $model->eduName = $this->getEduNameByEdu($model->education);
        $model->polityName = $this->getPolityNameByPolity($model->political_status);
        $model->marryName = $this->getMarryNameByMarry($model->marriage);
        $jobModel = JobModel::where('id',$model->job)->first();
        if ($jobModel) {
            $model->jobName = $jobModel->name;
        } else {
            $model->jobName = '';
        }
        $model->option = $this->getOneOptionByUid($model->uid);
        return $model;
    }

    /**
     * 学历数组
     */
    public function getEdus()
    {
        return $this->repository->getEdus();
    }

    /**
     * 婚姻数组
     */
    public function getMarrys()
    {
        return $this->repository->getMarrys();
    }

    /**
     * 政治面貌数组
     */
    public function getPolitys()
    {
        return $this->repository->getPolitics();
    }

    /**
     * 通过员工类型，岗位数组
     */
    public function getJobList($staffCate)
    {
        $models = JobModel::where('cate',$staffCate)
            ->where('pid',0)
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $key => $model) {
            $dataArr[$key]['id'] = $model->id;
            $dataArr[$key]['name'] = $model->name;
            $subs = JobModel::where('cate',$staffCate)
                ->where('pid',$model->id)
                ->get();
            $subArr = array();
            if (count($subs)) {
                foreach ($subs as $key2 => $sub) {
                    $subArr[$key2]['id'] = $sub->id;
                    $subArr[$key2]['name'] = $sub->name;
                }
            }
            $dataArr[$key] = $subArr;
        }
        return $dataArr;
    }

    /**
     * 学历
     */
    public function getEduNameByEdu($edu)
    {
//        $eduArr = array(
//            1 => '小学', '初中', '高中/中专/职高',
//            '本科', '研究生', '博士', '其他',
//        );
        $eduArr = $this->repository->getEdus();
        return array_key_exists($edu,$eduArr) ?
            $eduArr[$edu] : '';
    }

    /**
     * 婚姻
     */
    public function getMarryNameByMarry($marry)
    {
//        $marryArr = array(
//            1 => '未婚', '已婚', '离异', '丧偶',
//        );
        $marryArr = $this->repository->getMarrys();
        return array_key_exists($marry,$marryArr) ?
            $marryArr[$marry] : '';
    }

    /**
     * 政治面貌
     */
    public function getPolityNameByPolity($polity)
    {
//        $polityArr = array(
//            1 => '群众', '团员', '党员',
//        );
        $polityArr = $this->repository->getMarrys();
        return array_key_exists($polity,$polityArr) ?
            $polityArr[$polity] : '';
    }

    /**
     * 员工技能
     */
    public function getSkillList()
    {
        $models = CategoryModel::where('del',0)
            ->where('pid',129)
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $k=>$model) {
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['name'] = $model->name;
        }
        return $dataArr;
    }

    /**
     * 员工爱好
     */
    public function getHobbyList()
    {
        $models = CategoryModel::where('del',0)
            ->where('pid',130)
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $k=>$model) {
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['name'] = $model->name;
        }
        return $dataArr;
    }

    /**
     * 员工类型
     */
    public function getCateList()
    {
        return array(
            1 => '工厂员工', '公司员工',
        );
    }

    /**
     * 员工类型名称
     */
    public function getCateNameByCate($cate)
    {
        $cates = $this->getCateList();
        return array_key_exists($cate,$cates) ? $cates[$cate] : '';
    }

    /**
     * 性别
     */
    public function getSexList()
    {
        return array(
            1 => '男', '女',
        );
    }


    /**
     * -------------------------------------
     * 以下是信息2，选填信息
     * -------------------------------------
     */

    /**
     * 获取一条记录
     */
    public function getOneOptionByUid($uid)
    {
        $model = $this->option->where('uid',$uid)->first();
        $dataArr = array();
        if (!$model) {
            $dataArr = array(
                'eduBgs' => array(),
                'workHistorys' => array(),
                'familys' => array(),
                'comments' => array(),
            );
            return $dataArr;
        }
        $dataArr['eduBgs'] = $model->edu_bgs ?
            $this->objToArr($model->edu_bgs) : array();
        $dataArr['workHistorys'] = $model->work_historys ?
            $this->objToArr($model->work_historys) : array();
        $dataArr['familys'] = $model->familys ?
            $this->objToArr($model->familys) : array();
        $dataArr['comments'] = $model->comments ?
            $this->objToArr($model->comments) : array();
        return $dataArr;
    }

    /**
     * 员工公司信息选项数据
     */
    public function getComRadioList()
    {
        $types = array();
        $skills = array();
        //业务类型
        $model1s = CategoryModel::where('del',0)
            ->where('pid',135)->get();
        if (!count($model1s)) { $types = array(); }
        foreach ($model1s as $k=>$model1) {
            $types[$k]['id'] = $model1->id;
            $types[$k]['name'] = $model1->name;
        }
        //技术等级
        $model2s = CategoryModel::where('del',0)
            ->where('pid',136)->get();
        if (!count($model2s)) { $skills = array(); }
        foreach ($model2s as $k2=>$model2) {
            $skills[$k2]['id'] = $model2->id;
            $skills[$k2]['name'] = $model2->name;
        }
        return array(
            'jobTypes' => $types,
            'skills' => $skills,
        );
    }

    /**
     * 员工公司信息选项数据,更新
     */
    public function setOptionModifyByUid($uid,$cate,$data)
    {
        $model = $this->getOneOptionByUid($uid);
        $eduBgs = array(
            'eduBgStart' => $data['eduBgStart'],
            'eduBgEnd' => $data['eduBgEnd'],
            'eduBgSchool' => $data['eduBgSchool'],
            'eduBgMajor' => $data['eduBgMajor'],
            'eduBgAward' => $data['eduBgAward'],
            'eduBgCount' => $data['eduBgCount'],
        );
        $works = array(
            'workStart' => $data['workStart'],
            'workEnd' => $data['workEnd'],
            'workComName' => $data['workComName'],
            'workJob' => $data['workJob'],
            'workLeave' => $data['workLeave'],
            'workCount' => $data['workCount'],
        );
        $familys = array(
            'familyName' => $data['familyName'],
            'familyCall' => $data['familyCall'],
            'familyCom' => $data['familyCom'],
            'familyContact' => $data['familyContact'],
            'familyCount' => $data['familyCount'],
        );
        $comments = array(
            'comFeature' => $data['comFeature'],
            'comSkill' => $data['comSkill'],
            'comHobby' => $data['comHobby'],
            'comExpect' => $data['comExpect'],
            'businessRadioInput' => $data['businessRadioInput'],
            'skillRadioInput' => $data['skillRadioInput'],
        );
        if ($model) { //有则更新
            $resData = array(
                'edu_bgs' => json_encode($eduBgs),
                'work_historys' => json_encode($works),
                'familys' => json_encode($familys),
                'comments' => $comments ? json_encode($comments) : '',
                'updated_at' => time(),
            );
            StaffOptionModel::where('uid',$uid)->update($resData);
            return true;
        }
        //无则新增
        StaffOptionModel::create(array(
            'uid' => $uid,
            'edu_bgs' => json_encode($eduBgs),
            'work_historys' => json_encode($works),
            'familys' => json_encode($familys),
            'comments' => $comments ? json_encode($comments) : '',
            'created_at' => time(),
        ));
        return true;
    }

    /**
     * -------------------------------------
     * 选填信息
     * -------------------------------------
     */


    /**
     * -------------------------------------
     * 员工求职意向
     * -------------------------------------
     */

    /**
     * 公司求职意向，获取
     */
    public function getOneComWill($uid)
    {
        $model = $this->option->where('uid',$uid)->first();
        if (!$model->will_companys) { return array(); }
        $wills = json_decode($model->will);
        $tags = $wills['tags'] ? explode('||',$wills['tags']) : '';
        return array(
            'address' => $wills['address'],
            'job' => $wills['job'],
            'salary' => $wills['salary'],
            'workType' => $wills['workType'],
            'industry' => $wills['industry'],
            'jobTime' => $wills['jobTime'],
            'tags' => $tags,
        );
    }

    /**
     * 公司求职意向，设置
     */
    public function setComWillByUid($uid,$data)
    {
        StaffOptionModel::where('uid',$uid)
            ->update(array('will_companys'=>json_encode($data)));
        return true;
    }

    /**
     * 工厂求职意向，获取
     */
    public function getOneFacWill($uid)
    {
        $model = $this->option->where('uid',$uid)->first();
        if (!$model->will_factorys) { return array(); }
        $wills = json_decode($model->will_factorys);
        $fabric = $wills->fabricType ? $wills->fabricType : 0;
        $tech = $wills->techLevel ? $wills->techLevel : 0;
        $skill = $wills->skill ? $wills->skill : 0;
        return array(
            'fabrics' => $fabric,
            'tech' => $tech,
            'skills' => $skill,
        );
    }

    /**
     * 工厂求职意向，设置
     */
    public function setFacWillByUid($uid,$data)
    {
        StaffOptionModel::where('uid',$uid)
            ->update(array('will_factorys'=>json_encode($data)));
        return true;
    }

    /**
     * -------------------------------------
     * 员工求职意向
     * -------------------------------------
     */


    /**
     * -------------------------------------
     * 员工认证
     * -------------------------------------
     */

    /**
     * 员工认证，获取
     */
    public function getComFileList($uid)
    {
        $model = $this->option->where('uid',$uid)->first();
        if (!$model || !$model->files) { return array(); }
        return json_decode($model->files);
    }

    /**
     * 员工认证，获取2
     */
    public function getComFileJson($uid)
    {
        $model = $this->option->where('uid',$uid)->first();
        if (!$model || !$model->files) { return array(); }
        return $model->files;
    }

    /**
     * 员工认证，增加
     */
    public function setComFileAddByUid($uid,$data)
    {
        $dataArr = array(
            'uid' => $uid,
            'files' => json_encode($data),
            'created_at' => time(),
        );
        return $this->option->insertGetId($dataArr);
    }

    /**
     * 员工认证，设置
     */
    public function setComFileByUid($uid,$data)
    {
        $this->option->where('uid',$uid)->update(array(
            'files' => json_encode($data),
        ));
        return true;
    }

    /**
     * -------------------------------------
     * 员工认证
     * -------------------------------------
     */





    /**
     * -------------------------------------
     * 更新可选资料
     * -------------------------------------
     */

    /**
     * 获取可选资料
     */
    public function getOptionAdminByUid($uid)
    {
        $model = $this->option->where('uid', $uid)->first();
        $dataArr = $this->getOptionInit();
        if (!$model) { return $dataArr; }
        $dataArr['eduBgs'] = $model->edu_bgs ?
            $this->objToArr($model->edu_bgs) : $resOption['eduBgs'];
        $dataArr['workHistorys'] = $model->work_historys ?
            $this->objToArr($model->work_historys) :
            $resOption['workHistorys'];
        $dataArr['familys'] = $model->familys ?
            $this->objToArr($model->familys) : $resOption['familys'];
        $dataArr['comments'] = $model->comments ?
            $this->objToArr($model->comments) : $resOption['comments'];
        //工厂求职意向
        if ($model->will_factorys) {
            $willFacs = $this->objToArr($model->will_factorys);
            //面料种类
            if ($willFabrics=$willFacs['fabricType']) {
                $fabrics = $this->option->getFacFabricTypes();
                $fabricTypes = array();
                foreach ($willFabrics as $key => $willFabric) {
                    $fabricTypes[$key]['id'] = $willFabric;
                    $fabricTypes[$key]['name'] = array_key_exists($willFabric,$fabrics) ?
                        $fabrics[$willFabric] : '未定义';
                }
                $willFacs['fabricType'] = $fabricTypes;
            }
            //技术等级
            if ($willTech=$willFacs['techLevel']) {
                $techs = $this->option->getFacTechLevels();
                $techLevels['id'] = $willTech;
                $techLevels['name'] = array_key_exists($willTech,$techs) ?
                    $techs[$willTech] : '未定义';
                $willFacs['techLevel'] = $techLevels;
            }
            //擅长技能
            if ($willSkills=$willFacs['skill']) {
                $skills = $this->option->getFacSkills();
                $skillNews = array();
                foreach ($willSkills as $key => $willSkill) {
                    $skillNews[$key]['id'] = $willSkill;
                    $skillNews[$key]['name'] = array_key_exists($willSkill,$skills) ?
                        $skills[$willSkill] : '未定义';
                }
                $willFacs['skill'] = $skillNews;
            }
            $dataArr['willFacs'] = $willFacs;
        } else {
            $dataArr['willFacs'] = $resOption['willFacs'];
        }
        //公司求职意向
        if ($model->will_companys) {
            $dataArr['willComs'] = $this->objToArr($model->will_companys);
            //地址转换
            if ($addressStr=$dataArr['willComs']['address']) {
                $address = $this->getCityListByStr($addressStr);
                $dataArr['willComs']['addresss'] = $address;
            }
            //标签转换
        } else {
            $dataArr['willComs'] = $resOption['willComs'];
        };
        $dataArr['files'] = $model->files ?
            $this->objToArr($model->files) : $resOption['files'];
        return $dataArr;
    }

    /**
     * 员工工厂求职意向全数组
     */
    public function getOptionFacWills()
    {
        $fabrics = $this->option->getFacFabricTypes();
        $techs = $this->option->getFacTechLevels();
        $skills = $this->option->getFacSkills();
        return array(
            'fabrics' => $fabrics,
            'techs' => $techs,
            'skills' => $skills,
        );
    }

    /**
     * 设置可选资料
     */
    public function setOptionAdminByUid($uid,$data)
    {
        $model = $this->option->where('uid',$uid)->first();
        $dataArr = array(
            'edu_bgs' => json_encode($data['eduBgs']),
            'work_historys' => json_encode($data['workHistorys']),
            'familys' => json_encode($data['familys']),
            'comments' => json_encode($data['comments']),
            'will_factorys' => json_encode($data['willFacs']),
            'will_companys' => json_encode($data['willComs']),
            'files' => json_encode($data['files']),
        );
        if (!$model) {
            $dataArr['uid'] = $uid;
            $dataArr['created_at'] = time();
            return $this->option->insertGetId($dataArr);
        }
        $dataArr['updated_at'] = time();
        $this->option->where('id',$model->id)->update($dataArr);
        return $model->id;
    }

    /**
     * 获取可选资料的初始化格式
     */
    public function getOptionInit()
    {
        $eduBgs = array(
            'eduBgStart' => array(''),
            'eduBgEnd' => array(''),
            'eduBgSchool' => array(''),
            'eduBgMajor' => array(''),
            'eduBgAward' => array(''),
            'eduBgCount' => 0,
        );
        $works = array(
            'workStart' => array(''),
            'workEnd' => array(''),
            'workComName' => array(''),
            'workJob' => array(''),
            'workLeave' => array(''),
            'workCount' => 0,
        );
        $familys = array(
            'familyName' => array(''),
            'familyCall' => array(''),
            'familyCom' => array(''),
            'familyContact' => array(''),
            'familyCount' => 0,
        );
        $comments = array(
            'comFeature' => '',
            'comSkill' => '',
            'comHobby' => '',
            'comExpect' => '',
            'businessRadioInput' => '',
            'skillRadioInput' => '',
        );
        $willFacs = array(
            'fabricType' => '',
            'techLevel' => '',
            'skill' => '',
        );
        $willComs = array(
            'address' => '',
            'addresss' => array(
                'provice' => array('id'=>0, 'name'=>''),
                'city' => array('id'=>0, 'name'=>''),
                'area' => array('id'=>0, 'name'=>''),
            ),
            'jobModule' => '',
            'job' => '',
            'salary' => '',
            'workType' => '',
            'industry' => '',
            'jobTime' => '',
            'tags' => '',
        );
        $files = array(
            'selfs' => '',
            'idcards' => '',
            'byzs' => '',
            'awards' => '',
            'marrys' => '',
            'drives' => '',
        );
        return array(
            'eduBgs' => $eduBgs,
            'workHistorys' => $works,
            'familys' => $familys,
            'comments' => $comments,
            'willFacs' => $willFacs,
            'willComs' => $willComs,
            'files' => $files,
        );
    }
}